﻿function AzureOnlineBlob($initialInfo) {

    var self = this;

    var name = ko.observable('');

    var url = ko.observable('');

    var etag = ko.observable('');

    var cacheControl = ko.observable('');

    var contentEncoding = ko.observable('');

    var contentLanguage = ko.observable('');

    var contentLength = ko.observable('');

    var contentType = ko.observable('');

    var contentMD5 = ko.observable('');

    var contentMD5Hex = ko.computed({
        read: function () {
            var str = contentMD5();
            if (utils.isNullOrWhiteSpace(str)) return '';
            var b64 = new Base64().decode(str);
            return new HexString().format(b64);
        },
        write: function (val) {
            if (utils.isNullOrWhiteSpace(val)) {
                contentMD5('');
            }
            var array = new HexString().parse(val);
            var b64 = new Base64().encode(array);
            contentMD5(b64);
        }
    });

    var type = ko.observable('');

    var lastModified = ko.observable('');

    self.metadata = ko.observableArray([]);

    self.blobType = ko.computed(function () {
        switch (type()) {
            case 'BlockBlob':
                return 'Block';
            case 'PageBlob':
                return 'Page';
            default:
                return type();
        }
    });

    self.locked = ko.observable('');

    self.formattedLength = ko.computed(function () {
        var length = contentLength();
        if (typeof length == "string") {
            length = parseInt(length);
        }
        if (isNaN(length)) return 'N/A';
        if (length < 1024) return length.toString();
        length /= 1024;
        if (length < 1024) return (Math.round(length * 100) / 100).toString() + 'kb';
        length /= 1024;
        if (length < 1024) return (Math.round(length * 100) / 100) + 'mb';
        length /= 1024;
        return (Math.round(length * 100) / 100) + 'gb';
    });

    self.setInfo = function ($info) {
        name($info.find('Name').text());
        url($info.find('Url').text());
        etag($info.find('Etag').text());

        cacheControl($info.find('Cache-Control').text());
        contentEncoding($info.find('Content-Encoding').text());
        contentLanguage($info.find('Content-Language').text());
        contentLength($info.find('Content-Length').text());
        contentType($info.find('Content-Type').text());
        contentMD5($info.find('Content-MD5').text());

        lastModified($info.find('Last-Modified').text());

        self.locked($info.find('LeaseStatus').text() == 'locked');

        var blobType = $info.find('BlobType').text();
        type(blobType);

        var $meta = $info.find('Metadata').children();
        var meta = [];
        for (var i = 0; i < $meta.length; i++) {
            var $metaItem = $($meta[i]);
            var metaName = $metaItem.prop('tagName');
            var value = $metaItem.text();
            meta.push({
                name: metaName,
                value: value
            });
        }

        self.metadata(meta);
    };

    function clone() {
        var blob = new AzureOnlineBlob();
        cloneTo(blob);
        return blob;
    }

    function cloneTo(other) {
        other.name(name());
        other.url(url());
        other.etag(etag());
        other.type(type());
        other.cacheControl(cacheControl());
        other.contentEncoding(contentEncoding());
        other.contentLanguage(contentLanguage());
        other.contentLanguage(contentLanguage());
        other.contentType(contentType());
        other.contentMD5(contentMD5());
        other.contentLength(contentLength());
        other.lastModified(lastModified());
    }

    self.getPropertiesInfo = function () {
        return {
            cacheControl: cacheControl(),
            contentEncoding: contentEncoding(),
            contentLanguage: contentLanguage(),
            contentType: contentType(),
            contentMD5: contentMD5(),
            contentLength: contentLength()
        };
    };

    self.name = name;
    self.url = url;
    self.etag = etag;
    self.type = type;
    self.cacheControl = cacheControl;
    self.contentEncoding = contentEncoding;
    self.contentLanguage = contentLanguage;
    self.contentType = contentType;
    self.contentMD5 = contentMD5;
    self.contentMD5Hex = contentMD5Hex;
    self.contentLength = contentLength;
    self.lastModified = lastModified;

    self.clone = clone;
    self.cloneTo = cloneTo;

    if ($initialInfo) {
        self.setInfo($initialInfo);
    }

}